﻿using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

public class AuthResponsesOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        var authAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
            .Union(context.MethodInfo.GetCustomAttributes(true))
            .OfType<AuthAttribute>();

        operation.Parameters.Add(new OpenApiParameter()
        {
            Name = "platform",
            In = ParameterLocation.Header,
            Required = true,
        });

        if (authAttributes.Any())
        {
            operation.Parameters.Add(new OpenApiParameter()
            {
                Name = "access-token",
                In = ParameterLocation.Header,
                Required = true,
            });
            //operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" });
        }
    }
}